<?php
namespace app\controller;

use app\AuthController;
use think\facade\Db;      //使用Db数据库对象时需要引入的方法
use think\facade\View;
use think\facade\Session;
use think\facade\Cookie;

use app\model\{
	Article as ArticleModel,
	FriendShip as FriendShipModel,
	User as UserModel,
	PortalCategory as PortalCategoryModel,
	LinkSpace as LinkSpaceModel,
  CommentBoard as CommentBoardModel,
  ContentArticle as ContentArticleModel,
	Comment as CommentModel,
};
use app\BaseInfoController as BaseInfo;

class Comment extends AuthController
{

#############################################################################################################################################################

######################################################### 【留言功能】 评论提交 #################################################################################


public function read($id)
{
    // 获取此文章的留言信息
    $comment = CommentModel::where('far_id', $id)->where('prev','0')->order('time desc')->select()->toArray();
    $coreply = CommentModel::where('far_id', $id)->where('prev','<>','0')->order('time asc')->select()->toArray();
    foreach($comment as $key => $value){
        foreach($coreply as $k => $vol){
            if($vol['prev']==$value['id']){
                $comment[$key]['zclass'][] = $vol;
                unset($coreply[$k]);
            }
        }
    }

    $touristInfo = Cookie::get('tourist-info');
    View::assign([
      'comment' => $comment, //此文章评论
      'isAdmin' => $this->request->sellerId === 1,
      'touristInfo' => $touristInfo ? unserialize($touristInfo) : null,
    ]);

    return View::fetch('home_template/mz_comments', [
      'cms' => CommentBoardModel::select()->toArray(),
      'unick' => UserModel::getUserNickKey(), // 会员昵称UID索引
      'uport' => UserModel::getUserHportKey(), // 会员头像UID索引
      'erysts' => BaseInfo::everydayPhrases(), // 每日一句
      'user' => $this->request->adminInfo, // 网站信息
      'site_info' => BaseInfo::siteInfo(), // 网站信息
      'main_nav_menu' => BaseInfo::mainNavMenu(), // 主导航
      'vice_nav_menu' => BaseInfo::viceNavMenu(), // 副导航
      'menu' => null, // 首页导航ID
      'hotacl' => ArticleModel::getHot(), // 热门文章
      'article' => null,
  ]);
}

# 评论提交
public function save()
{
    // 获取登录用户信息
    $user    = $this->request->adminInfo;

    // 获取评论的内容
    $comment = $this->request->post('comment/s');
    $aid     = $this->request->articleId;
    $nick    = $this->request->post('nick/s');
    $email   = $this->request->post('email/s');
    $website = $this->request->post('website/s');
    $replyid = $this->request->post('reply/d');

    // 检查文章是否存在
    $is_has  = ArticleModel::where('id', $aid)->count();
    if(!$is_has)
        return json(['state'=>'error','info'=>'此文章或留言板不存在']);
    // 检测评论内容不为空
    if(empty($comment))
        return json(['state'=>'error','info'=>'评论内容不能为空']);
    // 检测昵称不为空
    if(!$user)
        if(empty($nick))
            return json(['state'=>'error','info'=>'昵称不能为空']);

    // return json(['state'=>'error','info'=>$comment.'--'.$aid.'--'.$nick.'--'.$email.'--'.$website.'--'.$replyid]);

    // 评论信息写入数据表
    if($replyid==0) {
        $prev  = 0;
        $sayto = null;
        $nick1 = null;
    }else{
        $previd = CommentModel::where('id',$replyid)->value('prev');
        if($previd==0)
            $prev = $replyid;
        else
            $prev = $previd;
        $sayto = CommentModel::where('id',$replyid)->value('unick');
    }

    if($user) {
      $commentData['unick'] = $user['account'];
      $commentData['hport'] = $user['hport'];
      $commentData['email'] = $user['email'];
      $commentData['website'] = $user['website'];
    } else {
      $commentData['unick'] = $nick;
      $commentData['email'] = $email;
      $commentData['website'] = $website;
      Cookie::set('tourist-info', serialize($commentData));
    }

    $commentData['far_id'] = $aid;
    $commentData['prev'] = $prev;
    $commentData['sayto'] = $sayto;
    $commentData['essay'] = $comment;
    $result = CommentModel::create($commentData);

    $data['state']   = 'success';
    $data['id']      = $result->id;
    $data['aid']     = $result->far_id;
    $data['nick']    = $result->unick;
    $data['retnick'] = $result->sayto;
    $data['email']   = $result->email;
    $data['website'] = $result->website  ;
    $data['essay']   = $result->essay;
    $data['time']    = $result->time;

    return json($data);
}
#############################################################################################################################################################






################################################## 撤回、修改、删除 ############################################################################################
# 撤回留言功能
public function json_comment_recall(){
  # 获取登录用户信息
  $user = $this->request->adminInfo;
  $id = $this->request->post('id/d');
  $time = date('Y-m-d H:i:s',time()-125);
  $result = CommentModel::whereTime('time','>=',$time)->delete($id);
  if($result)
      return json(['state'=>'success','info'=>'撤回成功']);
  else
      return json(['state'=>'error','info'=>'撤回失败，可能时间已超时哦~']);
}
# 修改留言
public function commentUpdate(){
  # 获取登录用户信息
  $user = $this->request->adminInfo;
  if($user['id'] != 1)
      return '<h2>您没有权限操作删除留言功能</h2>';

  if($this->request->isPost()){
      $data['id']    = $this->request->post('id/d');
      $data['time']  = $this->request->post('time/s');
      $data['essay'] = $this->request->post('essay/s');
      $result = CommentModel::update($data);
      if($result)
          $this->success('修改成功',url("/comment/update",array("id"=>$data["id"])),'',1);
      else
          $this->error('修改失败',url("/comment/update",array("id"=>$data["id"])),'',1);
  }else{
    return View::fetch('home_template/comment_update', [
      'result' => CommentModel::find($this->request->param('id/d')),
    ]);
  }
}
# 删除留言
public function json_comment_delete(){
  # 获取登录用户信息
  $user = $this->request->adminInfo;
  if($user['id'] == 1){
      $id = $this->request->post('id/d');
      $result = CommentModel::where('id',$id)->delete();
      if($result)
          return json(['state'=>'success','info'=>'删除成功']);
      else
          return json(['state'=>'error','info'=>'删除失败']);
  }else{
      return json(['state'=>'error','info'=>'<h2>您没有权限操作删除留言功能</h2>']);
  }
}
#############################################################################################################################################################






}
 